package com.lalameow.serverlogingate.command;

import com.lalameow.packet.pojo.MdsInfo;
import com.lalameow.packet.pojo.ServerInfo;
import com.lalameow.serverlogingate.enetiy.GameServerInfo;
import com.lalameow.serverlogingate.network.server.NetGateServer;
import com.lalameow.serverlogingate.network.server.NetGateState;
import com.lalameow.serverlogingate.service.GameServerInfoService;
import com.lalameow.serverlogingate.service.PlayerInfoService;
import com.lalameow.serverlogingate.utiles.ApplicationContextUtil;
import com.lalameow.serverlogingate.utiles.ModUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 陈刚
 * 日期: 2018/3/2
 * 时间: 0:45
 * 功能：请进行修改
 */
@ShellComponent
@Slf4j
public class NetGateCommand {

    @Autowired
    private NetGateServer netGateServer;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private ModUtils modUtils;
    @Autowired
    private GameServerInfoService gameServerInfoService;
    @Autowired
    private PlayerInfoService playerInfoService;
    @Autowired
    private NetGateCommand netGateCommand;
    @ShellMethod("启动网关.")
    public void start(){
        threadPoolTaskExecutor.submit(new Runnable() {
            @Override
            public void run() {
                if(netGateServer.getNetGateState()!= NetGateState.RUNNING){
                    netGateServer.start();
                }else {
                    log.warn("登录网关已经启动！");
                }
            }
        });
    }

    @ShellMethod("关闭网关.")
    public void stop(){
        netGateServer.stop();
    }
    @ShellMethod("扫描mod.")
    public void scanMods() throws IOException {
        modUtils.scanMods();
        log.info("扫描mod已经成功，已经加载了{}个mod信息:",modUtils.getVaildModList().size());
        for (MdsInfo mdsInfo : modUtils.getVaildModList()) {
            log.info(mdsInfo.getMdsName());
            log.info("   -MD5:"+mdsInfo.getMdsMd5());
            log.info("   -SHA1:"+mdsInfo.getMdsHash());
        }
    }
    @ShellMethod("锁定玩家.")
    public void locked(String playerName){
        playerInfoService.lockedPlayer(playerName);
    }
    @ShellMethod("切换玩家的是否通过mod检测.")
    public void switchTrust(String playerName){
        playerInfoService.switchTrust(playerName);
    }
    @ShellMethod("解锁玩家.")
    public void unlocked(String playerName){
        playerInfoService.unlockedPlayer(playerName);
    }
    @ShellMethod("添加游戏服务器.")
    public void addServer(String name,String host){
        gameServerInfoService.save(name, host);
    }
    @ShellMethod("删除游戏服务器.")
    public void delServer(String name){
        gameServerInfoService.delete(name);
        log.info("服务器{}删除成功。",name);
    }
    @ShellMethod("上线服务器.")
    public void online(String name){
        gameServerInfoService.online(name);
    }
    @ShellMethod("下线服务器.")
    public void offline(String name){
        gameServerInfoService.offline(name);
    }
    @ShellMethod("刷新服务器列表.")
    public void refreshserver(){
        GameServerInfoService.serverInfos=gameServerInfoService.getAllServer();
        log.info("一共加载{}线上服务器:",GameServerInfoService.serverInfos.length);
        for (ServerInfo serverInfo : GameServerInfoService.serverInfos) {
            log.info("  -{}:{}",serverInfo.getServerName(),serverInfo.getServerHost());
        }
    }

    @PostConstruct
    public void serverStart() throws IOException {
        scanMods();
        start();
        refreshserver();
    }

}
